from PySide6.QtCore import (QMetaObject, QRect, Qt)
from PySide6.QtWidgets import *

from config import ScriptList
from windows.EditWindow import EditWindow


class ScriptItem(QWidget):
    def __init__(self, data):
        super().__init__()
        self._editWindow = None
        self.setFixedSize(750, 50)
        self.setupUi()
        self.data = data

    def setupUi(self):
        self.labName = QLabel(self)
        self.labName.setObjectName(u"labName")
        self.labName.setGeometry(QRect(0, 10, 121, 41))
        self.labName.setAlignment(Qt.AlignCenter)
        self.labName.setWordWrap(True)

        self.labDescription = QLabel(self)
        self.labDescription.setObjectName(u"labDescription")
        self.labDescription.setGeometry(QRect(130, 10, 151, 41))
        self.labDescription.setAlignment(Qt.AlignCenter)
        self.labDescription.setWordWrap(True)

        self.labCreatedAt = QLabel(self)
        self.labCreatedAt.setObjectName(u"labCreatedAt")
        self.labCreatedAt.setGeometry(QRect(290, 10, 80, 41))
        self.labCreatedAt.setAlignment(Qt.AlignCenter)
        self.labCreatedAt.setWordWrap(True)

        self.labUpdatedAt = QLabel(self)
        self.labUpdatedAt.setObjectName(u"labUpdatedAt")
        self.labUpdatedAt.setGeometry(QRect(380, 10, 80, 41))
        self.labUpdatedAt.setAlignment(Qt.AlignCenter)
        self.labUpdatedAt.setWordWrap(True)

        self.btnRun = QPushButton(self)
        self.btnRun.setObjectName(u"btnRun")
        self.btnRun.setGeometry(QRect(500, 11, 40, 40))
        self.btnRun.clicked.connect(self.run)
        self.btnRun.setText('运行')

        self.btnModify = QPushButton(self)
        self.btnModify.setObjectName(u"btnModify")
        self.btnModify.setGeometry(QRect(550, 11, 40, 40))
        self.btnModify.clicked.connect(self.modify)
        self.btnModify.setText("编辑")

        self.btnDelete = QPushButton(self)
        self.btnDelete.setObjectName(u"btnDelete")
        self.btnDelete.setGeometry(QRect(600, 11, 40, 40))
        self.btnDelete.clicked.connect(self.delete)
        self.btnDelete.setText("删除")

        QMetaObject.connectSlotsByName(self)

    # setupUi

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, data):
        self._data = data
        self.refresh()

    def refresh(self):
        data = self.data
        self.labName.setText(data['name'])
        self.labDescription.setText(data['description'])
        self.labCreatedAt.setText(data['created_at'])
        self.labUpdatedAt.setText(data['updated_at'])

    def run(self):
        from runner.Runner import Runner
        Runner().run(self.data['content'])

    def modify(self):
        if not self._editWindow and self._editWindow is None:
            self._editWindow = EditWindow(self, self.data)

        self._editWindow.show()

    def delete(self):
        result = QMessageBox.question(self, '删除', '确认要删除脚本?')
        if result == QMessageBox.Yes:
            self.close()
            self.deleteLater()
            ScriptList.remove(self.data)
